﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;

public partial class Manage_TopicTree : System.Web.UI.Page
{
    protected string TreeText;

    protected void Page_Load(object sender, EventArgs ea)
    {
        StringBuilder builder = new StringBuilder();
        CmsDataContext db = new CmsDataContext();
        var q = from aT in db.Topic
                where aT.ParentID == null
                select aT;

        builder.Append("<div class=\"Tree\">");
        builder.Append(string.Format("<div><table><tr><td style=\"padding-left:{1}px\"><a href=\"javascript:void(0);\" topicid=\"{2}\">{0}</a></td></tr></table></div>", "全部栏目", 0,"null"));
        foreach (Topic t in q)
        {
            builder.Append(CreateNode(t, 1));
        }
        builder.Append("</div>");

        TreeText = builder.ToString();
    }

    protected string CreateNode(Topic topic, int level)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(string.Format("<div><table><tr><td style=\"padding-left:{1}px\"><a href=\"javascript:void(0);\" topicid=\"{2}\">{0}</a></td></tr></table></div>", topic.Name, level * 14, topic.ID));
        if (topic.Child != null && topic.Child.Count > 0)
        {
            sb.Append("<div>");

            foreach (Topic t in topic.Child)
            {
                sb.Append(CreateNode(t, level + 1));
            }

            sb.Append("</div>");
        }

        return sb.ToString();
    }
}